<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="商砼企业ID" prop="companyId">
        <el-input v-model="formData.companyId" placeholder="请输入商砼企业ID" />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目id" />
      </el-form-item>
      <el-form-item label="商品id" prop="goodsId">
        <el-input v-model="formData.goodsId" placeholder="请输入商品id" />
      </el-form-item>
      <el-form-item label="数量" prop="needNumber">
        <el-input v-model="formData.needNumber" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="已完成数量" prop="endNumber">
        <el-input v-model="formData.endNumber" placeholder="请输入已完成数量" />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input v-model="formData.province" placeholder="请输入省" />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入市" />
      </el-form-item>
      <el-form-item label="区" prop="area">
        <el-input v-model="formData.area" placeholder="请输入区" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="经度" prop="lon">
        <el-input v-model="formData.lon" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="维度" prop="lat">
        <el-input v-model="formData.lat" placeholder="请输入维度" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择开始时间"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择结束时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as Api from '@/api/system/purchase/'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  companyId: undefined,
  projectId: undefined,
  goodsId: undefined,
  needNumber: undefined,
  endNumber: undefined,
  province: undefined,
  city: undefined,
  area: undefined,
  address: undefined,
  lon: undefined,
  lat: undefined,
  startTime: undefined,
  endTime: undefined
})
const formRules = reactive({
  companyId: [{ required: true, message: '商砼企业ID不能为空', trigger: 'blur' }],
  projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
  goodsId: [{ required: true, message: '商品id不能为空', trigger: 'blur' }],
  needNumber: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  endNumber: [{ required: true, message: '已完成数量不能为空', trigger: 'blur' }],
  province: [{ required: true, message: '省不能为空', trigger: 'blur' }],
  city: [{ required: true, message: '市不能为空', trigger: 'blur' }],
  area: [{ required: true, message: '区不能为空', trigger: 'blur' }],
  address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
  lat: [{ required: true, message: '维度不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await Api.get(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Api.VO
    if (formType.value === 'create') {
      await Api.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await Api.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    companyId: undefined,
    projectId: undefined,
    goodsId: undefined,
    needNumber: undefined,
    endNumber: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    address: undefined,
    lon: undefined,
    lat: undefined,
    startTime: undefined,
    endTime: undefined
  }
  formRef.value?.resetFields()
}
</script>
